package com.jzy.bxb.manage.service;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jzy.bxb.manage.core.service.MongoBaseService;
import com.jzy.bxb.model.constant.AppConstant;
import com.jzy.bxb.model.mongo.dao.UserDao;
import com.jzy.bxb.model.mongo.entity.User;

/**
 * 用户
 *
 * @author JiangZhiYong
 * @date 2017-05-23 QQ:359135103
 */
@Service
public class UserService extends MongoBaseService<User, Long> {

	@Autowired
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
		setBaseDao(userDao);
	}

	/**
	 * 登录, 需要userName或validationEmail
	 * 
	 * @param param
	 * @return
	 */
	public User login(User param) {
		User result = null;
		if (param.getUserName() != null && param.getUserName().trim().length() > 0) {
			result = findOne("userName", param.getUserName());
		} else if (param.getPhone() != null && param.getPhone().trim().length() > 0) {
			result = findOne("validationEmail", param.getPhone());
		}
		// 查询并验证密码
		if (result != null &&result.isAdmin()&& result.getPassword().equals(param.getPassword())) {
			return result;
		}
		return null;
	}

	/**
	 * 存储 cookie
	 * 
	 * @param session
	 * @param muser
	 * @param response
	 */
	public void saveCookie(HttpSession session, User muser, HttpServletResponse response) {
		Cookie sessionCookie = new Cookie(AppConstant.USER, session.getId());
		sessionCookie.setMaxAge(3600);
		sessionCookie.setPath("/");
		response.addCookie(sessionCookie);

		Cookie loginidCookie = new Cookie(AppConstant.USER_NAME, muser.getUserName());
		// 30*24*60*60
		loginidCookie.setMaxAge(2592000);
		loginidCookie.setPath("/");
		response.addCookie(loginidCookie);
	}

	/**
	 * 删除
	 * 
	 * @param result
	 * @param password
	 */
	public void delete(User result, String password) {
		// 删除前验证密码
		if (result != null && result.getPassword().equals(password)) {
			delete(result);
		}
	}

	/**
	 * 获取所有用户
	 * 
	 * @return
	 */
	public List<User> getUsers() {
		Query<User> query = userDao.createQuery();
		return query.asList();
	}

	// /**
	// * 注册前检查
	// *
	// * @param param
	// * @return
	// */
	// public User register(User param) {
	// Query<User> query = createQuery();
	// query.or(query.criteria("userName").equal(param.getUserName()),
	// query.criteria("nickName").equal(param.getNickName()),
	// query.criteria("validationEmail").equal(param.getValidationEmail()));
	// User result = findOne(query);
	// // 是否有重名
	// if (result != null) {
	// return null;
	// }
	//
	// result = new User();
	// result.setUserName(param.getUserName());
	// result.setPassword(param.getPassword());
	// result.setNickName(param.getNickName());
	// result.setValidationEmail(param.getValidationEmail());
	// save(result);
	// return result;
	// }

}
